package com.wodogs.utils.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import us.codecraft.webmagic.utils.FilePersistentBase;

public class FileStore extends FilePersistentBase{
	
	public static Log logger = LogFactory.getLog(FileStore.class);
	
	public static String PATH_SEPERATOR = "/";
	
	static {
        String property = System.getProperties().getProperty("file.separator");
        if (property != null) {
            PATH_SEPERATOR = property;
        }
    }
	
	private String platformName;
	
	private String fileName;
	
	private String perfix="log";
	
	public static String tomcatPath = java.lang.System.getProperty("catalina.home");
	
	private String baseDir = tomcatPath+PATH_SEPERATOR+"logs"+PATH_SEPERATOR+"sprider"+PATH_SEPERATOR+"charge";
	
	public FileStore(String platformName,String fileName) {
		super();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		setPath(baseDir + PATH_SEPERATOR + sdf.format(new Date()));
		this.platformName = platformName;
		this.fileName = fileName;
	}

	public void append(String content){
		try {
			PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(
					getFileNamePath())), "UTF-8"));
			printWriter.println(content);
			printWriter.close();
		} catch (IOException e) {
			logger.warn("write file error", e);
			logger.error(e.getMessage(), e);
		}
	}
	
	public File getFile(){
		return getFile(getFileNamePath());
	}
	
	public void storePicture(File screenshot,String fileName){
		File file = getFile(getFileNamePath());
		try {
			FileUtils.copyFile(screenshot, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeLinesToFile(String filename, String[] linesToWrite, boolean appendToFile) {
		PrintWriter pw = null;
		try {
			if (appendToFile) {
				// If the file already exists, start writing at the end of it.
				pw = new PrintWriter(new FileWriter(filename, true));
			} else {
				pw = new PrintWriter(new FileWriter(filename));
				// this is equal to:
				// pw = new PrintWriter(new FileWriter(filename, false));
			}
			for (int i = 0; i < linesToWrite.length; i++) {
				pw.println(linesToWrite[i]);
			}
			pw.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// Close the PrintWriter
			if (pw != null)
				pw.close();
		}
	}
	
	private String getFileNamePath(){
		return getPath() + getFileName()+"."+getPerfix();
	}
	
	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getPlatformName() {
		return platformName;
	}

	public void setPlatformName(String platformName) {
		this.platformName = platformName;
	}

	public String getPerfix() {
		return perfix;
	}

	public void setPerfix(String perfix) {
		this.perfix = perfix;
	}

	public String getBaseDir() {
		return baseDir;
	}

	public void setBaseDir(String baseDir) {
		this.baseDir = baseDir;
	}

}
